package com.moji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.moji.entity.MjHomepage;
import com.moji.entity.MjProductDetail;
import com.moji.entity.vo.CommonProductDetailDTO;
import com.moji.mapper.MjHomepageMapper;
import com.moji.mapper.MjProductDetailMapper;
import com.moji.service.MjProductDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品详情 服务实现类
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@Service
public class MjProductDetailServiceImpl extends ServiceImpl<MjProductDetailMapper, MjProductDetail> implements MjProductDetailService {
    @Resource
    private MjProductDetailMapper mjProductDetailMapper;
    @Override
    public List<MjProductDetail> queryDetailByProductID(CommonProductDetailDTO commonProductDetailDTO) {
        LambdaQueryWrapper<MjProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        if (commonProductDetailDTO.getProductType() != null) {
            queryWrapper.eq(MjProductDetail::getProductType, commonProductDetailDTO.getProductType());
        }
        queryWrapper.eq(MjProductDetail::getProductId, commonProductDetailDTO.getProductId());
        List<MjProductDetail> page = this.list(queryWrapper);
        return page;
    }

    @Override
    public MjProductDetail queryById(Long id) {
        return this.mjProductDetailMapper.selectById(id);
    }

    @Override
    public MjProductDetail insert(MjProductDetail mjSolution) {
        LambdaQueryWrapper<MjProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProductDetail::getProductId,mjSolution.getProductId());
        queryWrapper.eq(MjProductDetail::getProductType,mjSolution.getProductType());
        List<MjProductDetail> mjProductDetailList = mjProductDetailMapper.selectList(queryWrapper);
        if (mjProductDetailList != null&&mjProductDetailList.size()>0) {
            mjSolution.setTypeName(mjProductDetailList.get(0).getTypeName());
            mjSolution.setDecrible(mjProductDetailList.get(0).getDecrible());
            mjSolution.setProductDuration(mjProductDetailList.get(0).getProductDuration());
        }

        this.mjProductDetailMapper.insert(mjSolution);
        return mjSolution;
    }

    @Override
    public MjProductDetail update(MjProductDetail mjSolution) {
        LambdaQueryWrapper<MjProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProductDetail::getId, mjSolution.getId());
        MjProductDetail mjProductDetail = this.mjProductDetailMapper.selectById(mjSolution.getId());
        if(TextUtils.isEmpty(mjSolution.getDecrible()))
        {
            mjSolution.setDecrible(mjProductDetail.getDecrible());
        }
        if(mjSolution.getProductId()==null)
        {
            mjSolution.setProductId(mjProductDetail.getProductId());
        }
        if(mjSolution.getProductType()==null)
        {
            mjSolution.setProductType(mjProductDetail.getProductType());
        }
        if(TextUtils.isEmpty(mjSolution.getTypeName()))
        {
            mjSolution.setTypeName(mjProductDetail.getTypeName());
        }
        if(TextUtils.isEmpty(mjSolution.getProductDuration()))
        {
            mjSolution.setProductDuration(mjProductDetail.getProductDuration());
        }
        if(TextUtils.isEmpty(mjSolution.getProductPreferential()))
        {
            mjSolution.setProductPreferential(mjProductDetail.getProductPreferential());
        }
        mjSolution.setUpdateTime(new Date());
        this.mjProductDetailMapper.update(mjSolution, queryWrapper);
        return this.queryById(mjSolution.getId());
    }

    @Override
    public Boolean deleteById(Long id) {
        return this.mjProductDetailMapper.deleteById(id) > 0;
    }


}
